/**
 * 初始化对话框
 */
var VpnAdsManageDlg = {
    VpnAdsManageData : {}
};

/**
 * 关闭此对话框
 */
VpnAdsManageDlg.close = function() {
    parent.layer.close(window.parent.VpnAdsManage.layerIndex);
}

VpnAdsManageDlg.updateAd = function() {

    var adId = $("#adId").val();
    var adName = $("#adName").val();
    var adLocation = $("#adLocation").val();
    var startTime = $("#startTime").val();
    var endTime = $("#endTime").val();
    var jumpUrl = $("#jumpUrl").val();
    var crow_file = $("#crowdImg")[0].files[0];

    var showType = "";
    var arr =[];

    $("input[name='showType']:checked").each(function (i) {
        arr.push($(this).val());
    });
    if (arr.length < 1){
        Feng.error("请选择展示人群")
        return;
    }
    for (let i = 0; i < arr.length; i++) {
        showType = showType + arr[i] + ",";
    }
    showType = showType.substring(0,showType.length-1);

    var formData =  new FormData;

    if(crow_file != null){
        formData.append("file",crow_file);
    }
    formData.append("adId",adId);
    formData.append("adName",adName);
    formData.append("adLocation",adLocation);
    formData.append("startTime",startTime);
    formData.append("endTime",endTime);
    formData.append("jumpUrl",jumpUrl);
    formData.append("showType",showType);

    var confirmIndex = layer.confirm("确定修改广告信息?",
        {btn:['确认修改','取消修改']},
        function () {
            layer.closeAll()
            var loadIndex = layer.load(0,{
                shade:false
            });

            setTimeout(function () {
                submitAdInfo(formData);
            },1000);

        },function () {
            layer.msg("取消操作",{icon:1})
        });
}


function submitAdInfo(formData){
    $.ajax({
        url: Feng.ctxPath + "/vpnAdsManage/update",
        dataType:'json',
        type:'POST',
        async: false,
        data: formData,
        processData : false, // 使数据不做处理
        contentType : false, // 不要设置Content-Type请求头
        success: function(data){
            console.log(data);
            if(data.code == 200){
                Feng.success("广告信息修改成功!");
                window.parent.VpnAdsManage.refresh();
                VpnAdsManageDlg.close();
            }
            layer.closeAll();
        },
        error:function(response){
            console.log(response);
            Feng.error(response.responseJSON.message);
            layer.closeAll();
        }
    });
}


$(function() {
});
